package Application.Tool;

import Application.AppJoint;
import Application.Controller.DarkenSettingsController;
import Application.Mask.MaskManager;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;
import Application.LayerImage.LayerImage;
import javafx.scene.input.MouseEvent;
import Application.Layer.Layer;
import Application.Layer.LayerDelegate;

public class Darken extends Tool {
    static private Darken ins = new Darken();
    private Parent root;
    private int[][] cache;

    // 蒙版
    private LayerImage mask;
    // 大小
    private int size = 32;
    // 增量
    private double delta = 50;

    private int blend(int old, double grow, int i, int j, int gray) {
        int a0 = (old >> 24) & 0xff;
        int r0 = (old & 0x00ff0000) >> 16;
        int g0 = (old & 0x0000ff00) >> 8;
        int b0 = (old & 0x000000ff);

        grow = gray * grow / 255;
        //System.out.println(grow);
        int r = 0, g = 0, b = 0;
        r = r0 - (int)grow;
        if(r < 0) {
            r = 0;
        }
        g = g0 - (int)grow;
        if(g < 0) {
            g = 0;
        }
        b = b0 - (int)grow;
        if(b < 0) {
            b = 0;
        }
        cache[i][j] += grow;

        //System.out.println((a0 << 24) | (r << 16) | (g << 8) | b);
        return (a0 << 24) | (r << 16) | (g << 8) | b;
        //return b;
    }

    private Darken() {
        super();

        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/DarkenSettings.fxml"));
            root = loader.load();

            AppJoint.getInstance().loadMasksToComboBox(
                    ((DarkenSettingsController)loader.getController()).getCombo()
            );
        } catch(Exception e) {
            e.printStackTrace();
        }

        mask = MaskManager.getInstance().getMaskByName("test");
    }

    static public Darken getInstance() {
        return ins;
    }

    @Override public void onMouseMove(MouseEvent e) {
        double pixelx =
                e.getX() / AppJoint.getInstance().getImageView().getFitWidth() * AppJoint.getInstance().getCanvasWidth();
        double pixely =
                e.getY() / AppJoint.getInstance().getImageView().getFitHeight() * AppJoint.getInstance().getCanvasHeight();
        Layer activeLayer = LayerDelegate.getInstance().getLayerById(AppJoint.getInstance().getActiveLayerId());

        for(int i = -size / 2; i < size / 2; i++) {
            for(int j = -size / 2; j < size / 2; j++) {
                // 实际像素位置
                int imgx = (int)pixelx + i;
                int imgy = (int)pixely + j;
                if(imgx >= 0 && imgx < AppJoint.getInstance().getCanvasWidth() && imgy >= 0 && imgy < AppJoint.getInstance().getCanvasHeight()) {
                    // 蒙版像素位置
                    int maskx = (i + size / 2) * MaskManager.MASK_SIZE / size;
                    int masky = (j + size / 2) * MaskManager.MASK_SIZE / size;
                    // 蒙版灰度
                    int gray = (mask.getPixelArgb(maskx, masky) & 255);

                    if(cache[imgx][imgy] < delta) {
                        double grow = Math.min(delta - cache[imgx][imgy], delta);

                        int oldColor = activeLayer.getArgb(imgx, imgy);
                        int newColor = this.blend(oldColor, grow, imgx, imgy, gray);

                        activeLayer.setArgb(imgx, imgy, newColor);
                        LayerDelegate.getInstance().refreshSingle(imgx, imgy);
                    }
                }
            }
        }
    }

    @Override public void onMouseDown(MouseEvent e) {
//        int activeLayerId = AppJoint.getInstance().getActiveLayerId();
//        Layer activeLayer = LayerDelegate.getInstance().getLayerById(activeLayerId);
//        cache = activeLayer.cloneLayerImage();
        int width = AppJoint.getInstance().getCanvasWidth();
        int height = AppJoint.getInstance().getCanvasHeight();
        cache = new int[width][height];

        onMouseMove(e);
    }

    @Override public void onMouseUp(MouseEvent e) {

    }

    @Override public Parent getToolSettings() {
        return root;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void setMask(String maskName) {
        this.mask = MaskManager.getInstance().getMaskByName(maskName);
    }

    public void setDelta(double delta) {
        this.delta = delta;
    }
}
